package org.jmh.org.controller;

import org.jmh.auth.domain.Menu;
import org.jmh.auth.dto.RoleDTO;
import org.jmh.base.annotation.JmhPermission;
import org.jmh.base.util.AjaxResult;
import org.jmh.base.util.PageInfo;
import org.jmh.org.domain.Department;
import org.jmh.org.domain.Employee;
import org.jmh.org.query.DepartmentQuery;
import org.jmh.org.query.EmployeeQuery;
import org.jmh.org.service.IDepartmentService;
import org.jmh.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

//@Controller
@Api(value = "员工管理",description = "员工操作的CRUD")
@RequestMapping("/employee")
@RestController  // 该controller所有的接口均返回数据
@JmhPermission(name="员工管理",desc = "管理员工的CRUD")
public class EmployeeController {
    @Autowired
    private IEmployeeService service;

    // 查全部 get
    @GetMapping
    @JmhPermission(name="员工查询",desc = "管理员工的查询所有")
    public AjaxResult selectList() {
        // 有返回值
        try {
            List<Employee> employees = service.selectAll();
            return AjaxResult.success(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //员工保存
    @ApiOperation(value = "更新或新增")
    @PutMapping
    @JmhPermission(name="员工保存",desc = "管理员工的新增和修改")
    public AjaxResult save(@RequestBody  Employee department){
        try {
            if (Objects.isNull(department.getId())){
                service.insert(department);
            }else{
                service.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    // 删 delete
    @DeleteMapping("/{id}")
    @JmhPermission(name="员工删除",desc = "管理员工的删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        // 有返回值
        try {
            service.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 查一个 get
    @GetMapping("/{id}")
    @JmhPermission(name="员工查询",desc = "根据员工id查询指定员工")
    public AjaxResult selectByID(@PathVariable("id") Long id){
        try {
            Employee employee = service.selectById(id);
            return AjaxResult.success(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    // 分页查询 post
    @PostMapping
    @JmhPermission(name="分页查询",desc = "员工的分页查询")
    public AjaxResult queryPage(@RequestBody EmployeeQuery employeeQuery){
        try {
            PageInfo pageInfo = service.queryPage(employeeQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 批量删除 delete
    @PatchMapping
    @JmhPermission(name="批量删除",desc = "员工的批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        // 有返回值
        try {
            service.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/menu/{id}")
    public AjaxResult getMenuByEmpId(@PathVariable("id")Long id)
    {
        try {
            List<Menu> list = service.getMenuByEmpId(id);
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("失败！"+e.getMessage());
        }
    }

    //    角色提交
    @PutMapping("/auth1")
    public AjaxResult auth1(@RequestBody RoleDTO roleDTO){
        try {
            service.auth1(roleDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }

    //    根据id赋予菜单
    @GetMapping("/role/{id}")
    public AjaxResult queryRoleById(@PathVariable("id")Long id){

        try {
            List<Long> ids = service.queryRoleById(id);
            return AjaxResult.success(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }
}
